package com.lancoo.znbkxx.teacher.net;


import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.lancoo.cpbase.authentication.base.CurrentUser;
import com.lancoo.cpbase.authentication.base.LoginOperate;
import com.lancoo.znbkxx.teacher.Ext;
import com.lancoo.znbkxx.teacher.base.BaseResponse;
import com.lancoo.znbkxx.teacher.model.ErrorRecoveryEntity;
import com.lancoo.znbkxx.teacher.model.PaperEvaluationEntity;
import com.lancoo.znbkxx.teacher.model.PaperForEvalEntity;
import com.lancoo.znbkxx.teacher.model.PaperInforEntity;
import com.lancoo.znbkxx.teacher.model.QuesAnalysisEntity;
import com.lancoo.znbkxx.teacher.model.StuPaperInforEntity;
import com.lancoo.znbkxx.teacher.model.data.ClassStuInfo;
import com.lancoo.znbkxx.teacher.model.data.GrammarInfo;
import com.lancoo.znbkxx.teacher.model.data.GrammarTrailInfo;
import com.lancoo.znbkxx.teacher.model.data.LanguageCognitionInfo;
import com.lancoo.znbkxx.teacher.model.data.LanguageTrailInfo;
import com.lancoo.znbkxx.teacher.model.data.PlatesDetailInfo;
import com.lancoo.znbkxx.teacher.model.data.PlatesInfo;
import com.lancoo.znbkxx.teacher.model.data.StuGradeForCenterInfo;
import com.lancoo.znbkxx.teacher.model.data.StuGradeInfo;
import com.lancoo.znbkxx.teacher.model.data.StuTopicInfo;
import com.lancoo.znbkxx.teacher.model.data.StuVocabInfo;
import com.lancoo.znbkxx.teacher.model.data.StudyAdviceInfo;
import com.lancoo.znbkxx.teacher.model.data.StudyTraitInfo;
import com.lancoo.znbkxx.teacher.model.data.TrailInfo;
import com.lancoo.znbkxx.teacher.model.data.TraitInfo;
import com.lancoo.znbkxx.teacher.model.data.UnstudyTopicInfo;
import com.lancoo.znbkxx.teacher.model.data.UnstudyVocabInfo;
import com.lancoo.znbkxx.teacher.model.data.VocabTrailInfo;
import com.lancoo.znbkxx.teacher.model.data.VocabularyInfo;
import com.lancoo.znbkxx.teacher.model.data.ZnbkResponse;
import com.lancoo.znbkxx.teacher.model.data.ZsdArea;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;



/**
 * Created by Dadong on 2020/4/2 21:47
 *
 * @Describe: 用rxjava对接口方法封装
 */

public class BkApiMethods {
    private Context mContext;
    private LoginOperate operate;

    public BkApiMethods(Context context) {
        mContext = context;
        operate = new LoginOperate(mContext);
    }

    /*
     * 记录操作日志
     * */
    public Single<ResponseBody> saveOperation(String basUrl, JSONObject jSONObject) {
        String BODY = new Gson().toJson(jSONObject);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), BODY);
        return BkApi.getApiService(basUrl).saveOperation(body)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 获取试卷预览详情
     * */
    public Single<BaseResponse<PaperInforEntity>> getPreviewPaperInfor(String basUrl, Map<String, String> params) {
        return BkApi.getApiService(basUrl).getPreviewPaperInfor(params)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }


    /*
     * 获取单题试题作答统计信息
     * */
    public Single<BaseResponse<QuesAnalysisEntity>> getSingleQuesDetail(String basUrl, Map<String, String> params) {
        return BkApi.getApiService(basUrl).getSingleQuesDetail(params)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 获取待评阅试题
     * */
    public Single<BaseResponse<List<PaperForEvalEntity>>> getPaperForEval(String basUrl, Map<String, String> params) {
        return BkApi.getApiService(basUrl).getPaperForEval(params)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 获取学生个人试卷作答详情
     * */
    public Single<BaseResponse<StuPaperInforEntity>> getStudentPaperInfo(String basUrl, Map<String, String> params) {
        return BkApi.getApiService(basUrl).getStudentPaperInfo(params)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 获取试卷讲评信息
     * */
    public Single<BaseResponse<PaperEvaluationEntity>> getPaperComment(String basUrl, Map<String, String> params) {
        return BkApi.getApiService(basUrl).getPaperComment(params)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 讲评试卷导出路径
     * */
    public Single<BaseResponse<String>> exportCommentPath(String basUrl, JSONObject jSONObject) {
        String BODY = new Gson().toJson(jSONObject);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), BODY);
        return BkApi.getApiService(basUrl).exportCommentPath(body)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 获取纠错选项
     * */
    public Single<BaseResponse<List<ErrorRecoveryEntity>>> getErrorRecovery(String basUrl) {
        return BkApi.getApiService(basUrl).getErrorRecovery()
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 提交纠错结果
     * */
    public Single<BaseResponse<String>> submitErrorRecovery(String basUrl, JSONObject jSONObject) {
        String BODY = new Gson().toJson(jSONObject);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), BODY);
        return BkApi.getApiService(basUrl).submitErrorRecovery(body)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /*
     * 提交纠错结果
     * */
    public Single<BaseResponse> saveStuQuesTeacherScore(String basUrl, RequestBody postBody) {

        return BkApi.getApiService(basUrl).saveStuQuesTeacherScore(postBody)
                .doOnSubscribe(disposable -> {
                    if (operate.tokenCheck(Ext.Companion.getPLATFORM_URL()) != 1)
                        operate.reLoginReWrite();
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Single<ZnbkResponse<TraitInfo>> getStuExerciseTrait(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuExerciseTrait(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<GrammarInfo>> getStuGrammar(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuGrammar(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<StuTopicInfo>> getStuTopicInfoList(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuTopicInfoList(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<GrammarTrailInfo>> getStuGrammarRZPlate(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuGrammarRZPlate(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<LanguageCognitionInfo>> getLanguageOutputs(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getLanguageOutputs(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<LanguageTrailInfo>> getLanguageHisChange(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getLanguageHisChange(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<PlatesInfo>> getStuCognitiveIndex(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuCognitiveIndex(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<List<PlatesDetailInfo>>> getStuCognitivePlate(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuCognitivePlate(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<StudyTraitInfo>> getStudyFeature(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStudyFeature(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<UnstudyTopicInfo>> getStuUnStudyTopicZsdInfoList(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuUnStudyTopicZsdInfoList(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<UnstudyVocabInfo>> getStuUnStudyVocabInfoList(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuUnStudyVocabInfoList(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<StuVocabInfo>> getStuVocabInfoList(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuVocabInfoList(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<VocabTrailInfo>> getStuVocabRZPlate(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuVocabRZPlate(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<VocabularyInfo>> getStuVocabulary(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuVocabulary(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<TrailInfo>> getStuPerformTrail(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuPerformTrail(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<StudyAdviceInfo>> getStudyAdvice(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStudyAdvice(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<List<ZsdArea>>> getZsdArea(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getZsdArea(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    /*开启个人认知诊断*/
    public Single<ZnbkResponse<Boolean>> isOpenStuRZZD(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .isOpenStuRZZD(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<ClassStuInfo>> getStuInfoByClass(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getStuInfoByClass(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<StuGradeInfo>> getCognitiveGradeWithUserInfo(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .getCognitiveGradeWithUserInfo(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    public Single<ZnbkResponse<StuGradeForCenterInfo>> getCognitiveGradeForCenter(String basUrl, Map<String, Object> params) {
        return BkApi.getApiService(basUrl)
                .GetCognitiveGradeForCenter(params)
                .compose(BkSchedulerProvider.getInstance(mContext).applySchedulers());
    }

    /*
     * 自定义Get公共传参
     * */
    public static HashMap<String, Object> Paramas() {
        HashMap<String, Object> paramas = new HashMap<>();
        paramas.put("Key", Ext.Companion.getKEY());
        paramas.put("Token", CurrentUser.getToken());
        paramas.put("SchoolID", CurrentUser.getSchoolID());
        paramas.put("CourseClassID", Ext.Companion.getCOURSE_CLASSID());
        paramas.put("GlobalGrade", Ext.Companion.getGLOBAL_GRADE());
        paramas.put("TID", CurrentUser.getUserID());
        paramas.put("UserID", CurrentUser.getUserID());
        paramas.put("SubjectID", "C");
        paramas.put("ReplaceBlank", true);
        paramas.put("ReplaceUrl", true);
        paramas.put("StartIndex", 1);
        paramas.put("StageNo", Ext.Companion.getSTAGE_NO());
        return paramas;

    }

    /*
     * 自定义Post公共传参
     * */
    public static JSONObject createJSONObject() {
        JSONObject Msg = new JSONObject();
        Msg.put("Token", CurrentUser.getToken());
        Msg.put("Key", Ext.Companion.getKEY());
        Msg.put("UserID", CurrentUser.getUserID());
        Msg.put("SchoolID", CurrentUser.getSchoolID());
        Msg.put("GlobalGrade", Ext.Companion.getGLOBAL_GRADE());
        Msg.put("CourseClassID", Ext.Companion.getCOURSE_CLASSID());
        Msg.put("SubjectID", "C");
        Msg.put("ReplaceBlank", true);
        Msg.put("ReplaceUrl", true);
        Msg.put("StartIndex", 1);
        // 输出对象
        System.out.println("jsonObject：" + Msg);
        return Msg;
    }

}
